package com.sz.app.supplier.web.security;

import com.sz.biz.app.web.security.AbstractPrincipalManager;
import com.sz.common.base.constants.AppDomain;
import com.sz.common.core.service.Principal;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-05-19 14:03:00.
 */
@Service("webPrincipalManager")
public class SupplierWebPrincipalManager extends AbstractPrincipalManager {

    public static final String ASSOCIATED_ACCOUNT_ID = "AssociatedAccountId";
    public static final String ASSOCIATED_ACCOUNT_NAME = "AssociatedAccountName";

    /**
     * @param appDomain
     * @param accountId
     * @param accountName
     * @param props
     * @return
     */
    @Override
    protected Principal createPrincipal(int appDomain, int accountId, String accountName, Map<String, Object> props) {
        Principal principal = new SupplierWebPrincipal(AppDomain.SUPPLIER, accountId, accountName);
        if (props != null) {
            if (props.containsKey(ASSOCIATED_ACCOUNT_ID)) {
                principal.setAssociatedAccountId((int) props.get(ASSOCIATED_ACCOUNT_ID));
            }
            if (props.containsKey(ASSOCIATED_ACCOUNT_NAME)) {
                principal.setAssociatedAccountName((String) props.get(ASSOCIATED_ACCOUNT_NAME));
            }
        }
        return principal;
    }

    @Override
    public boolean isAnonymousSupported() {
        return true;
    }

    @Override
    public Principal createAnonymousPrincipal() {
        Principal principal = new SupplierWebPrincipal(AppDomain.SUPPLIER, 0, "Anonymous");
        return principal;
    }
}
